package elijah.base.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.BasePopupView;

import elijah.base.databinding.ViewClearEdittextBinding;
import elijah.base.databinding.ViewEditDialogBinding;
import elijah.base.dialog.BaseDialogView;
import elijah.base.dialog.BaseItemCreateView;
import elijah.base.dialog.api.OnActionCallback;

public class EditTextDialog extends BaseItemCreateView.IContentView<String, ViewEditDialogBinding> {


    public EditTextDialog(@NonNull Context context) {
        super(context);

    }

    @Override
    public void onSubmit(OnActionCallback<String> callback, BasePopupView dialog) {
        callback.onSubmit(getText(), dialog);

    }

    public EditTextDialog setInputType(int inputType) {
        getEditText().setInputType(inputType);
        return this;
    }

    public EditTextDialog setText(String text) {
        getEditText().setText(text);
        getEditText().setSelection(getEditText().length());
        return this;
    }

    public EditText getEditText() {
        return binding.clearEditText.getBinding().editText;
    }

    public String getText() {
        return binding.clearEditText.getText();
    }

    public void show(String title, OnActionCallback<String> onActionCallback) {
        show(0.4f, title, onActionCallback);
    }

    public void show(float width, String title, OnActionCallback<String> onActionCallback) {
        show(width, title, onActionCallback, null);

    }

    public void show(float width, String title, OnActionCallback<String> onActionCallback, BaseDialogView.PopupConfig popupConfig) {
        new BaseItemCreateView.Builder<String>(getContext())
                .setTitle(title)
                .setViewWidth(width)
                .setContentView(this)
                .setActionCallback(new OnActionCallback.Impl<String>() {
                    @Override
                    public void onSubmit(String value, BasePopupView dialog) {
                        onActionCallback.onSubmit(value, dialog);
                    }
                })
                .setPopupConfig(popupConfig)
                .show();
    }
}
